package jiyun.com.yiyanmeng;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import base.BaseActivity;
import bean.CodeBean;
import presenter.MissPassPresenter;
import utils.ToastUtil;
import view.MissPassView;

public class MissPassActivity extends BaseActivity<MissPassView, MissPassPresenter> implements MissPassView, View.OnClickListener {


    private ImageView mIvBack;
    /**
     * +86
     */
    private TextView mTxt1;
    private View mView1;
    /**
     * 请输入手机号
     */
    private EditText mEtPhone;
    /**
     * 请输入验证码
     */
    private EditText mEtCode;
    private View mView2;
    /**
     * 获取验证码
     */
    private TextView mTvMissGetcode;
    /**
     * 请输入密码
     */
    private EditText mEtPwd;
    /**
     * 确定
     */
    private Button mBtSubmitFindpass;
    private String mPhone;
    private int time;
    private Timer timer;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            mTvMissGetcode.setText(what + "秒后重新获取");
            if (what == 0) {
                timer.cancel();
                mTvMissGetcode.setClickable(true);
                mTvMissGetcode.setText("获取验证码");

            }


        }
    };
    private int myCode;


    @Override
    public void showToast(String msg) {
        ToastUtil.show(this, msg);
    }

    @Override
    public void onSuccess(Void voiddata) {
        showToast("找回密码成功");
        //修改密码成功之后跳转到登录页面并且关闭本页面
        startActivity(new Intent(this,LoginActivity.class));
    }

    @Override
    public void onSuccessCode(CodeBean data) {
        int code = data.getCode();
        showToast("验证码发送成功");
        time = 60;
        myCode = code;
        mTvMissGetcode.setClickable(false);
        //创建一个定时器60秒后才能继续发送验证码
        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                time--;
                Message obtain = Message.obtain();
                obtain.what = time;
                handler.sendMessage(obtain);

            }
        };
        timer.schedule(timerTask, 1000, 1000);
    }

    @Override
    public void onFail(String error) {
        showToast(error);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void initView() {
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mIvBack.setOnClickListener(this);
        mTxt1 = (TextView) findViewById(R.id.txt1);
        mView1 = (View) findViewById(R.id.view1);
        mEtPhone = (EditText) findViewById(R.id.et_phone);
        mEtCode = (EditText) findViewById(R.id.et_code);
        mView2 = (View) findViewById(R.id.view2);
        mTvMissGetcode = (TextView) findViewById(R.id.tv_miss_getcode);
        mTvMissGetcode.setOnClickListener(this);
        mEtPwd = (EditText) findViewById(R.id.et_pwd);
        mBtSubmitFindpass = (Button) findViewById(R.id.bt_submit_findpass);
        mBtSubmitFindpass.setOnClickListener(this);
    }

    @Override
    protected MissPassPresenter createPresenter() {
        return new MissPassPresenter();
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_miss_pass;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_miss_getcode:
                //根据输入框的手机号获取验证码
                mPhone = mEtPhone.getText().toString().trim();
                if (mPhone.isEmpty()) {
                    showToast("手机号不能为空");
                } else {
                    //如果手机号不为空则获取验证码
                    mPresenter.requestYanzhengma(mPhone, "2");
                }
                break;
            case R.id.bt_submit_findpass:
                //根据输入框的手机号、验证码、新密码修改密码
                String etcode = mEtCode.getText().toString().trim();
                String pass = mEtPwd.getText().toString().trim();
                int code;
                if (etcode.isEmpty()) {
                    showToast("请输入验证码");
                    code = Integer.parseInt(etcode);
                    if (myCode == 0 || myCode!=code) {
                        ToastUtil.show(this, "验证码不正确！");
                        return;
                    }
                } else if (pass.isEmpty()) {
                    showToast("请设置新密码");
                } /*else */else {
                    //如果手机号、验证码、新密码均不为空，则请求更改密码的接口
                    mPresenter.requestFindpass(mPhone, pass, pass, etcode);
                }
                break;
        }
    }

}
